package com.everyline.baselibrary.http;

import android.net.ParseException;

import com.blankj.utilcode.util.LogUtils;

import org.json.JSONException;

import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
import java.net.UnknownServiceException;

import retrofit2.HttpException;

/**
 * crated by Administrator on 2021/6/3
 * 746608431@qq.com
 * 描述：TODO
 */
public class RxExceptionUtils {
    /**
     * 未知错误
     */

    public static final int UNKNOWN = 1000;


    /**
     * 解析错误
     */

    public static final int PARSE_ERROR = 1001;


    /**
     * 网络错误
     */

    public static final int NETWORK_ERROR = 1002;


    /**
     * 协议错误
     */

    public static final int HTTP_ERROR = 1003;


    public static ApiException handleException(Throwable e) {

        LogUtils.d(e);

        ApiException ex;

        if (e instanceof ParseException || e instanceof JSONException) {

            //解析错误

            ex = new ApiException(PARSE_ERROR, e.getMessage());

            return ex;

        } else if (e instanceof ConnectException) {

            //网络错误

            ex = new ApiException(NETWORK_ERROR, "网络异常，请检查网络设置");

            return ex;

        } else if (e instanceof UnknownHostException || e instanceof SocketTimeoutException || e instanceof UnknownServiceException) {

            //连接错误

            ex = new ApiException(NETWORK_ERROR, "网络异常，请检查网络设置");

            return ex;

        } else if (e instanceof HttpException) {

            ex = new ApiException(((HttpException) e).code(), "网络异常，请检查网络设置");

            return ex;

        } else {

            //未知错误

            ex = new ApiException(UNKNOWN, e.getMessage());

            return ex;

        }

    }

}
